<?php


namespace app\admin\controller;


use app\admin\model\SystemPlanningTasks;
use think\admin\Controller;

/**
 * 定时任务管理
 * Class Menu
 * @package app\admin\controller
 */
class PlanningTasks extends Controller
{

    private $table = 'system_planning_tasks' ;

    /**
     * 定时任务列表
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
//        $jobHandlerClassName  = 'CheckWorkJob';
//        // 2.当前任务归属的队列名称，如果为新队列，会自动创建
//        $jobQueueName  	  = "helloJobQueue";
//        // 3.当前任务所需的业务数据 . 不能为 resource 类型，其他类型最终将转化为json形式的字符串
//        //   ( jobData 为对象时，需要在先在此处手动序列化，否则只存储其public属性的键值对)
//        $jobData       	  = [ 'name'=>'zhangsan'.time()] ;
//        // 4.将该任务推送到消息队列，等待对应的消费者去执行
//        $isPushed = \think\facade\Queue::push( $jobHandlerClassName , $jobData , $jobQueueName );
//        // database 驱动时，返回值为 1|false  ;   redis 驱动时，返回值为 随机字符串|false
//        if( $isPushed !== false ){
//            echo date('Y-m-d H:i:s') . " a new Hello Job is Pushed to the MQ"."<br>";
//        }else{
//            echo 'Oops, something went wrong.';
//        }
        $this->title = '定时任务列表';
        $this->type = input('name', 'index');
        $query = $this->_query($this->table);
        $query->like('name')->order('id desc')->page();
    }

    /**
     * 新增定时任务
     * @auth true
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = [
                'name' => input('name'),
                'cmd' => input('cmd'),
                'desc' => input('desc'),
                'parameter' => input('parameter'),
                'do_time' => input('do_time'),
                'type' => input('type'),
                'status' => input('status'),
                'update_time' => time(),
                'create_time' => time()
            ];
            $check_name = $this->app->db->name($this->table)->where(['name' => input('name')])->find();
            if ($check_name) {
                $this->error('新增失败，重复任务名');
            }
            $check_cmd = $this->app->db->name($this->table)->where(['cmd' => input('cmd')])->find();
            if ($check_cmd) {
                $this->error('新增失败，重复命令');
            }
            $result = $this->app->db->name($this->table)->insert($data);
            if (!$result) {
                $this->error('操作失败');
            }
            sysoplog('新增定时任务', '新增id为' . $result);
            $this->success('新增成功');
        }
        $this->_form($this->table, 'add');
    }

    /**
     * 修改定时任务
     * @auth true
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $id = input('id');
            $data = [
                'name' => input('name'),
                'cmd' => input('cmd'),
                'desc' => input('desc'),
                'parameter' => input('parameter'),
                'do_time' => input('do_time'),
                'type' => input('type'),
                'status' => input('status'),
                'update_time' => time()
            ];
            $where[] = ['id','<>',$id];
            $where[] = ['name','=',$data['name']];
            $where[] = ['cmd','=',$data['cmd']];
            $check_name = $this->app->db->name($this->table)->where($where)->find();
            if ($check_name) {
                $this->error('编辑失败，重复任务名');
            }
            $check_cmd = $this->app->db->name($this->table)->where($where)->find();
            if ($check_cmd) {
                $this->error('编辑失败，重复命令');
            }
            $result = $this->app->db->name($this->table)->where(['id' => input('id')])->update($data);
            if (!$result) {
                $this->error('操作失败');
            }
            sysoplog('编辑定时任务', '编辑id为' . $result);
            $this->success('编辑成功');
        }
        $this->_form($this->table, 'edit');
    }

    /**
     * 删除
     * @throws \think\db\exception\DbException
     * @auth true
     * @login true
     */
    public function remove()
    {
        sysoplog('删除定时任务', '删除id为' . input('id'));
        $this->_delete($this->table);
    }

}